package com.imodia.checkoo.webtag;

import java.io.IOException;
import java.io.Writer;
import java.util.Date;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.hsq.commons.util.BeanFactory;
import com.hsq.commons.util.DateUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.SqlParam;
import com.imodia.checkoo.bean.CmsOfferJobBean;
import com.imodia.checkoo.service.CmsOfferJobService;


public class JobPageTag extends RequestContextAwareTag{
	private static final long serialVersionUID = -1764906844122921796L;
	private TldParam tldParam;
	private PageBean pageParam;

	public TldParam getTldParam() {
		return tldParam;
	}

	public void setTldParam(TldParam tldParam) {
		this.tldParam = tldParam;
	}

	public PageBean getPageParam() {
		return pageParam;
	}

	public void setPageParam(PageBean pageParam) {
		this.pageParam = pageParam;
	}

	@Override
	protected int doStartTagInternal() {
		CmsOfferJobService cmsOfferJobService = (CmsOfferJobService) BeanFactory.getBean("cmsOfferJobService");
		
		SqlParam offerJoinSQL=new SqlParam();
		if(tldParam.getAreaId()>0 ){
			offerJoinSQL.andSQL(" id in(select job_id from cms_job_area where area_id = "+tldParam.getAreaId()+")");
		}
		String date = DateUtil.toString(new Date(), "yyyy.MM.dd");
		offerJoinSQL.andSQL(" ext_2 >= '"+date + "' and ext_1 <= '"+date+"' and checked = 1 ");
		
		PageResult<CmsOfferJobBean> pageList = cmsOfferJobService.page(offerJoinSQL,pageParam);

		pageContext.setAttribute("JobPageTag", pageList);
		
			Writer out = pageContext.getOut();
			StringBuffer buf = new StringBuffer();
			try {
				if (tldParam.getHtmls() != null && tldParam.getHtmls().trim().length() > 0) {
					buf.append(TldUtil.convertJob(pageList.getData(), tldParam));
				}
				buf.append("<input name='totalSize' type='hidden' id='totalSize' value='"+pageList.getTotalSize()+"' />");
				buf.append("<input name='pageSize' type='hidden' id='pageSize' value='"+pageList.getPageSize()+"' />");
				buf.append("<input name='pages' type='hidden' id='pages' value='"+pageList.getPages()+"' />");
				buf.append("<input name='currentPage' type='hidden' id='currentPage' value='"+pageList.getCurrentPage()+"' />");
				out.write(buf.toString());
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			out = null;
		return EVAL_BODY_INCLUDE;
	}
}
